﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace StarForce {
    public class UiMotionRotationCard : UiMotionBaseCard
    {
        public UiMotionRotationCard(Card handler) : base(handler) {
        }

        protected override float Threshold => 0.05f;

        protected override void OnMotionEnds() {
            Handler.transform.eulerAngles = Target;
            IsOperating = false;
            base.OnMotionEnds();
        }

        protected override bool CheckFinslState() {
            var distance = Target - Handler.transform.eulerAngles;
            var smallerThanLimit = distance.magnitude <= Threshold;
            var equals360 = (int) distance.magnitude == 360;
            var isFinal = smallerThanLimit || equals360;
            return isFinal;
        }

        protected override void KeepMotion() {
            var current = Handler.transform.rotation;
            var amount = Speed * Time.deltaTime;
            var rotation = Quaternion.Euler(Target);
            var newRotation = Quaternion.RotateTowards(current, rotation, amount);
            Handler.transform.rotation = newRotation;
        }
    }
}

